package com.infoland.afanmall.base.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.infoland.afanmall.R;
import com.infoland.afanmall.base.util.ToolsKit;
import com.infoland.afanmall.model.my.fragment.HdFragment;
import com.infoland.afanmall.model.my.fragment.WlFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class WelcomeBannerActivity extends AppCompatActivity {
    @BindView(R.id.welcome_view_page)
    ViewPager welcomeViewPage;
    List<Fragment> mFragments;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.welcome_banner_activity);
        ButterKnife.bind(this);
        mFragments = new ArrayList<>();
        mFragments.add(new View1("http://img07.tooopen.com/images/20170316/tooopen_sy_201956178977.jpg"));
        mFragments.add(new View2("http://img07.tooopen.com/images/20170316/tooopen_sy_201956178977.jpg"));
        welcomeViewPage.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return mFragments.get(position);
            }

            @Override
            public int getCount() {
                return mFragments.size();
            }

        });
        welcomeViewPage.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 1) {
                    ToolsKit.go(MainActivity.class);
                    finish();
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @SuppressLint("ValidFragment")
    public class View1 extends Fragment {
        String img_url = "";

        public View1(String img_url) {
            this.img_url = img_url;
        }

        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            return inflater.inflate(R.layout.welcome_view, container, false);
        }

        @Override
        public void onActivityCreated(@Nullable Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            ImageView viewById = getView().findViewById(R.id.welcome_view_img);
            ToolsKit.setImgActivity(WelcomeBannerActivity.this, img_url, viewById);
//            Glide.with(this).load(R.drawable.gif1)
//                    .diskCacheStrategy(DiskCacheStrategy.ALL).into(ivGif);
        }
    }

    @SuppressLint("ValidFragment")
    public class View2 extends Fragment {
        String img_url = "";

        public View2(String img_url) {
            this.img_url = img_url;
        }

        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            return inflater.inflate(R.layout.welcome_view, container, false);
        }

        @Override
        public void onActivityCreated(@Nullable Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            ImageView viewById = getView().findViewById(R.id.welcome_view_img);
            ToolsKit.setImgActivity(WelcomeBannerActivity.this, img_url, viewById);
        }
    }

    @Override
    public void onBackPressed() {
    }
}
